<template>
	<section>
		<!--高级查询-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true" >
				<el-form-item>
					<el-input v-model="keyword" placeholder="请输入关键字"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" v-on:click="keywordQuery">查询</el-button>
				</el-form-item>
				<!--<el-form-item>
					<el-button type="primary" @click="handleAdd">新增</el-button>
				</el-form-item>-->
			</el-form>
		</el-col>

		<!--列表-->
		<el-table :data="users" :row-key="getRowKey" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
			<el-table-column type="selection" :reserve-selection="true" width="55">
			</el-table-column>
			<el-table-column type="index" label="序号"  width="60">
			</el-table-column>
			<el-table-column prop="username" label="用户名称" width="140" sortable>
			</el-table-column>
			<el-table-column prop="phone" label="用户电话" width="140" sortable>
			</el-table-column>
			<el-table-column prop="email" label="用户邮箱" width="200" sortable>
			</el-table-column>
			<el-table-column prop="state" label="状态" width="80"  sortable>
				<template slot-scope="scope">
					<span style="color: red" v-if="scope.row.state==0">禁用</span>
					<span style="color: green" v-else-if="scope.row.state==1">启用</span>
					<span style="color: grey" v-else>未知</span>
				</template>
			</el-table-column>
			<el-table-column prop="age" label="年龄" width="150" sortable>
			</el-table-column>
			<el-table-column prop="createtime" label="注册时间" min-width="150" sortable>
			</el-table-column>
			<el-table-column label="操作" width="150">
				<template slot-scope="scope">
					<el-button type="success"  size="small" @click="handleEdit(scope.$index, scope.row)">修改状态</el-button>
					<!--<el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>-->
				</template>
			</el-table-column>
		</el-table>

		<!--工具条-->
		<el-col :span="24" class="toolbar">
			<el-pagination layout="prev, pager, next" @current-change="handleCurrentChange" :page-size="pageSize" :total="totals" :current-page="currentPage" style="float:right;">
			</el-pagination>
		</el-col>

		<!--编辑/新增界面-->
		<el-dialog :title="title" :visible.sync="saveFormVisible" :close-on-click-modal="false">
			<el-form :model="saveForm" label-width="280px" :rules="saveFormRules" ref="saveForm">
				<!--<el-form-item label="编号" prop="sn">
					<el-input v-model="saveForm.sn" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="姓名" prop="name">
					<el-input v-model="saveForm.name" auto-complete="off"></el-input>
				</el-form-item>-->
				<el-form-item label="状态">
					<el-radio-group v-model="saveForm.state">
						<el-radio class="radio" :label="1">启用</el-radio>
						<el-radio class="radio" :label="0">禁用</el-radio>
					</el-radio-group>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="saveFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="saveSubmit" :loading="saveLoading">提交</el-button>
			</div>
		</el-dialog>

	</section>
</template>

<script>
	import util from '../../common/js/util'
	//import NProgress from 'nprogress'
	import { getUserListPage, removeUser, batchRemoveUser, editUser, addUser } from '../../api/api';

	export default {
		data() {
			return {
                users: [],
                totals: 0,//总数
                pageSize:5,//每页数量
                currentPage:1,//页数
                listLoading: false,//加载框
                title: '',//标题
				//部门经理
                managers:[],
				//部门树
                deptTree:[],

				//高级查询参数
				keyword:'',

				sels: [],//列表选中列

				saveFormVisible: false,//编辑/新增界面是否显示
				saveLoading: false,
				saveFormRules: {
					sn: [
						{ required: true, message: '你写点东西会累死吗？', trigger: 'blur' }
					],
                    name: [
                        { required: true, message: '你输入了一团空气', trigger: 'blur' }
                        ]
				},
				//编辑/新增界面数据
				saveForm: {
                    id: null,
                    username:'',
                    phone:'',
                    email:'',
                    password:"",
                    state:1,
                    age:null,
                    createtime:null,
                    headImg:null,
                    logininfoId:null
				},

			}
		},
		methods: {
		    //高级查询
            keywordQuery(){
              this.currentPage = 1;
              this.getUsers()
			},
			//分页实现
            handleCurrentChange(val) {
                this.currentPage = val;
                this.getUsers();
            },
			//获取部门列表
			getUsers() {
				let para = {
                    pageSize: this.pageSize,
                    currentPage: this.currentPage,
					keyword:this.keyword
				};
				//开启加载框
				this.listLoading = true;
				//发送请求
				this.$http.post("/user",para).then(res=>{
				    //给users数组赋值
				    this.users = res.data.data;
				    //给分页赋值
                    this.totals = res.data.totals;
                    //关闭加载框
                    this.listLoading = false;
				}).catch(res=>{
					this.$message.error("系统繁忙，正在骚扰程序猿")
				})

			},
			//删除
			/*handleDel: function (index, row) {
				this.$confirm('确认删除该记录吗?', '提示', {
					type: 'warning'
				}).then(() => {
                    this.listLoading = true;//显示加载框
					this.$http.delete("/user/"+row.id).then(res=>{
                        this.listLoading = false;
                        if (res.data.success){
							this.$message.success("删除成功")
                            //如果当前页没有数据，那就跳转到前一页
                            if (this.menus.length == 1 && this.currentPage != 1) {
                                this.currentPage = this.currentPage - 1;
                            }
                            this.getUsers()
						}else {
                            this.$message.error("系统繁忙【500】")
						}
					}).catch(res=>{
						this.$message.error("系统繁忙【404.400】")
					})
				}).catch(() => {

				});
			},*/
			//显示编辑界面
            handleEdit: function (index, row) {
                this.title = "修改状态";
                //获取经理数据
                this.getManagers();
                //获取部门树
				this.getdeptTree();
                //弹出对话框
                this.saveFormVisible = true;
                //回显数据：创建一个新的空对象{}，然后将row中的数据拷贝一份给{}
                this.saveForm = Object.assign({}, row);
            },
			/*//显示新增界面
			handleAdd: function () {
                this.title = "新增";
                //获取经理数据
                this.getManagers();
                //获取部门树
                this.getdeptTree();
				this.saveFormVisible = true;
				this.saveForm = {
                    id: null,
                    sn: '',
                    name: '',
                    state: 1,
                    manager_id: null,
                    parent_id: null
				};
			},*/
			//编辑/新增
			saveSubmit: function () {
				this.$refs.saveForm.validate((valid) => {
					if (valid) {
						this.$confirm('确认提交吗？', '提示', {}).then(() => {
							this.saveLoading = true;
                            let para = Object.assign({}, this.saveForm);//数据赋值
                            //对上级部门的处理
                            var result = para.parent_id;

                            //5种情况，不修改值为2，点击后不修改【2】，去掉上级部门【】，选择子部门【2,3,4】，直接清空null
							if (Array.isArray(result)){
							    if (result.length==null){//空的
							        para.parent_id = null;
								}else {//有数据就获取数据
							        para.parent_id = result[result.length -1];
								}
							}
                           /* //如果为null,[] 就赋值为null，后端Long可以接收为null
                            if(result==null ||  result.length==0 ){
                                para.parent_id = null;
                            }
                            //如果有值：[ 1 ],[ 2, 5, 6 ]  或直接一个数字
                            if(result.length){ //没有选过就是之前的部门的话，result.length为undefined，判断为false
                                para.parent_id = result[result.length-1];
                            }else{
                                para.parent_id = result;
                            }*/
							this.$http.put("/user",para).then(res=>{
                                this.saveLoading = false;//关闭忙等框
                                this.saveFormVisible = false;//关闭会话框
							    if (res.data.success){
								  this.$message.success("保存成功");
                                  this.getUsers()
								}else {
							        this.$message.error("毁灭把，赶紧的【500】")
								}
							}).catch(rse=>{
							    this.$message.error("啊哈哈哈哈，出错了【400.404】")
							})
						});
					}
				});
			},
			selsChange: function (sels) {
				this.sels = sels;
			},
			//获取经理数据
    		getManagers(){
                this.$http.get("/employee").then(res=>{
                    this.managers = res.data;
				})
			},
			//获取部门树
            getdeptTree(){
                this.$http.get("/user/deptTree").then(res=>{
                    this.deptTree = res.data;
                })
            },
            getRowKey(row){
                return row.id;
            }
		},
		mounted() {
			this.getUsers();
		}
	}

</script>

<style scoped>

</style>